package com.cloudspace.jindun.activity.xiaoxi;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.model.GroupsDetail;
import com.cloudspace.jindun.network.url.YXHttpUrl;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ToastMaker;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

//加入群聊
public class GroupchatActivity extends CustomTitleBaseActivity {

    @Bind(R.id.groupchat_iv_back)
    ImageView mGroupchatIvBack;
    @Bind(R.id.phone_group_et_title)
    EditText mPhoneGroupEtTitle;
    @Bind(R.id.phone_btn_group_joinIn)
    Button mPhoneBtnGroupJoinIn;

    String qunID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xiaoxi_qunliao_joinin, false);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.groupchat_iv_back, R.id.phone_btn_group_joinIn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.groupchat_iv_back:
                finish();
                break;
            case R.id.phone_btn_group_joinIn:
                qunID=mPhoneGroupEtTitle.getText().toString();
                if (!TextUtils.isEmpty(qunID)) {
                    requestData(YXHttpUrl.XX_JRQZ, UserSessionManager.getInstance().getUserId(),qunID);
                }
                break;

            default:
                break;
        }
    }
    private void requestData(String url,String userId,String channelId){
        OkHttpUtils.post()
                .url(url)
                .addParams("userId",userId)
                .addParams("channelId",channelId)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        GroupsDetail groupsDetail = gson.fromJson(response, GroupsDetail.class);
                        if (groupsDetail.getStatus() == 200) {
                            ToastMaker.showToastShort(R.string.group_add_success);

                            Intent intent = new Intent();
                            intent.putExtra("delete", true);
                            setResult(RESULT_OK, intent);

                            finish();
                        }else{
                            ToastMaker.showToastShort(R.string.group_add_fail);
                        }
                    }
                });
    }
}
